const express = require('express')
const fs = require('fs')
const path = require('path')
const Mock = require('mockjs')
const router = express.Router()

// 登录
router.post('/', (req, res) => {
  const {username, password} = req.body

  if (['admin', 'teacher', 'parent'].includes(username)) {
    // 读取菜单权限文件
    const menus = JSON.parse(fs.readFileSync(path.join(__dirname, `../mock/${username}.json`)) + '')
    res.json({
      code: 200,
      data: Mock.mock({
        username,
        avatarUrl: '@image(64x64, @color, A)',
        token: Mock.Random.guid() + "-" + username,
        menus
      })
    })
  } else {
    res.json({
      code: 400,
      data: {
        message: '用户名或密码错误'
      }
    })
  }
})

// 退出
router.delete('/', (req, res) => {
  // 清理工作
  // TODO...........

  res.json({
    code: 200,
    data: {
      message: '退出成功'
    }
  })
})

module.exports = router
